package com.g3.zoosite.controller;


import com.g3.zoosite.common.Result;
import com.g3.zoosite.model.DwAnimalNews;
import com.g3.zoosite.service.IDwAnimalNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 动物趣闻表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-09-28
 */

@Api(tags = "动物趣闻接口")
@Slf4j  //日志打印
@RestController
@RequestMapping("/zoosite/dwAnimalNews")
public class DwAnimalNewsController {

    @Autowired
    private IDwAnimalNewsService dwAnimalNewsService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/loadAnimalNews")
    @ApiOperation("查询全部动物趣闻")
    public List<DwAnimalNews> getDwAnimalNews(){
        List<DwAnimalNews> animalNews = dwAnimalNewsService.findAnimalNews();
        return animalNews;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    @ApiOperation("通过id查询动物趣闻")
    public ModelAndView getById(@PathVariable Integer id){
        DwAnimalNews animalNew = dwAnimalNewsService.findAnimalNewsById(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/hfy/hfy_dwqw_detail");
        mv.addObject("animalNew",animalNew);
        return mv;
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("通过id删除")
    public Result deleteBuId(@ApiParam(value = "id") @PathVariable Integer id){
        boolean b = dwAnimalNewsService.removeById(id);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 插入数据
     * @param model
     * @return
     */
    @PostMapping
    @ApiOperation("插入数据")
    public Result insert(@ApiParam(name = "动物趣闻对象",value = "传入json格式") @RequestBody DwAnimalNews model){
        boolean b = dwAnimalNewsService.save(model);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 通过id修改
     * @param model
     * @return
     */
    @PutMapping
    @ApiOperation("通过id修改数据")
    public Result update(@ApiParam(name = "动物趣闻对象",value = "传入json格式") @RequestBody DwAnimalNews model){
        boolean b = dwAnimalNewsService.updateById(model);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }


}
